import { Controller } from "egg";
var id = 1;
var data = [{
  id:1,
  name:'张三'
}]

export default class newsController extends Controller {
  public async list() {
    // const { ctx } = this;
    this.success(data);
  }
  public async add() {
    const { ctx } = this;
    let row = ctx.request.body;
    row.id = ++id;
    data.push(row);
    this.success(row);
    // ctx.body = ctx.request.body;
  }

  public async edit() {
    const { ctx } = this;
    let id = ctx.request.body.id;
    let index = data.findIndex((row) => {
      return row.id == id;
    });
    data[index] = ctx.request.body;
    this.success(data[index]);
    // ctx.body = ctx.request.body;
  }

  public async deleteRow() {
    const { ctx } = this;
    let id = ctx.request.body.id;
    let index = data.findIndex((row) => {
      return row.id == id;
    });
    ;
    this.success(data.splice(index,1));
    // ctx.body = ctx.request.body;
  }

  success(data: any) {
    this.ctx.body = {
      code: 0,
      msg: "success",
      data,
    };
  }
}
